﻿using System;
using Books.Interfaces;
using Books.Model;
using RefLib.Data;

namespace Books.DAL
{
    public class BooksRepository : IBooksDataService, IDisposable
    {
        public void Dispose()
        {
            if (_booksContext != null)
            {
                _booksContext.Dispose();
                _booksContext = null;
            }
        }

        public int CreatePublisher(PublisherModel model)
        {
            return DB.CreatePublisher(model);
        }

        public void DeletePublisher(int publisherId)
        {
            DB.DeletePublisher(publisherId);
        }

        public BookModel[] GetAllBooks()
        {
            return DB.GetBooks();
        }

        public PublisherModel[] GetAllPublishers(string sort)
        {
            return DB.GetPublishers(sort);
        }

        public BookModel GetBookById(int bookId)
        {
            return DB.GetBookById(bookId);
        }

        public PublisherModel GetPublisherById(int publisherId)
        {
            return DB.GetPublisherById(publisherId);
        }

        public PublisherModel[] MatchPublishers(string publisherName, string city)
        {
            return DB.MatchPublishers(publisherName, city);
        }

        public void UpdatePublisher(PublisherModel model)
        {
            DB.UpdatePublisher(model);
        }

        internal BooksContext DB
        {
            get
            {
                if (_booksContext == null)
                    _booksContext = ContextBuilder.Build<BooksContext>(BooksContext.ConnectionName);
                return _booksContext;
            }
        }

        private BooksContext _booksContext;
    }
}
